package com.weather.pm.utils;

import java.net.URL;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.tencent.mm.sdk.platformtools.Util;
import com.weather.pm.volley.cache.BitmapLruCache;

public class WXShareUtil {
	
	private static int THUMB_SIZE = 100;
//	private static final String WEIXIN_APP_ID = "wx1eede2cbfe8e5c55"; // 閸欐垵绔�?	
	public static final String WEIXIN_APP_ID = "wx2261aaf44bbad0c5"; // Debug
	private static IWXAPI wxapi;
	private static Context mContext;

	public WXShareUtil() {
	}
	
	/**
	 * 濞夈劌鍞�?	 * @param context 
	 * @author XieWenjun
	 */
	public static void registerToWX(Context context) {
		mContext = context;
		wxapi = WXAPIFactory.createWXAPI(context, WEIXIN_APP_ID, true);
		wxapi.registerApp(WEIXIN_APP_ID);
	}
	
	/**
	 * 閸欐垿锟界拠閿嬬�?
	 * @param mediaMessage
	 * @param transaction
	 * @param isTimeline 
	 * @author XieWenjun
	 */
	private static boolean sendReq(WXMediaMessage mediaMessage, String transaction,
			boolean isTimeline) {
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction(transaction);
		req.message = mediaMessage;
		req.scene = isTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
		return wxapi.sendReq(req);
	}
	
	/**
	 * 閺傚洤鐡�?	 * @param text
	 * @param isTimeline 
	 * @author XieWenjun
	 */
	public static boolean shareText(String text, boolean isTimeline) {
		WXTextObject textObject = new WXTextObject();
		textObject.text = text;
		
		WXMediaMessage mediaMessage = new WXMediaMessage();
		mediaMessage.mediaObject = textObject;
		mediaMessage.description = text;
		
		return sendReq(mediaMessage, "text", isTimeline);
	}
	
	/**
	 * 閸ュ墽澧�bitmap
	 * @param bitmap
	 * @param isTimeline
	 * @author XieWenjun
	 */
	public static boolean sharePhoto(Bitmap bitmap, boolean isTimeline,String title,String content) {
		WXImageObject imageObject = new WXImageObject(bitmap);
		
		WXMediaMessage mediaMessage = new WXMediaMessage();
		mediaMessage.mediaObject = imageObject;
		Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
		bitmap.recycle();
		mediaMessage.thumbData = Util.bmpToByteArray(thumbBitmap, true);
		mediaMessage.title = title;
		mediaMessage.description = content;
		return sendReq(mediaMessage, "img", isTimeline);
	}
	
	/**
	 * 閸ュ墽澧�url
	 * @param url
	 * @param isTimeline 
	 * @author XieWenjun
	 */
	public static boolean sharePhoto(String url, boolean isTimeline,String title,String content) {
		WXImageObject imageObject = new WXImageObject();
		imageObject.imageUrl = url;
		
		WXMediaMessage mediaMessage = new WXMediaMessage();
		mediaMessage.mediaObject = imageObject;
		mediaMessage.title = title;
		mediaMessage.description = content;
		Bitmap bmp = null;
		try {
			bmp = BitmapFactory.decodeStream(new URL(url).openStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
		bmp.recycle();
		mediaMessage.thumbData = Util.bmpToByteArray(thumbBmp, true);
		
		return sendReq(mediaMessage, "img", isTimeline);
	}
	
	/**
	 * 缂冩垿銆�?	 * @param url
	 * @param title
	 * @param description
	 * @param isTimeline 
	 * @author XieWenjun
	 */
	@SuppressLint("NewApi")
	public static boolean shareWebPage(String url, Bitmap bmp,String title, String description, boolean isTimeline) {
		WXWebpageObject webpageObject = new WXWebpageObject();
		webpageObject.webpageUrl = url;
		
		WXMediaMessage mediaMessage = new WXMediaMessage();
		mediaMessage.mediaObject = webpageObject;
		mediaMessage.title = title;
		mediaMessage.description = description;
        mediaMessage.thumbData = Util.bmpToByteArray(bmp, true);
		return sendReq(mediaMessage, "webpage", isTimeline);
	}
	
	/**
	 * transaction鐎涙顔岄悽銊ょ艾閸烆垯绔撮弽鍥槕娑擄拷閲滅拠閿嬬�?
	 * @author XieWenjun
	 */
	private static String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}

}
